<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/26
 * Time: 16:10
 * 会员卡管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;

class Membership extends OperationBase {

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 会员
     */
    public function member() {
        $auth_group = sp_get_auth_menu_3("20011000");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $api_versions = db("api_template")->field("id, name, api_version")->select();
        $this->assign("api_versions", $api_versions);
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        echo $this->fetch();
    }

    /**
     * 会员列表数据
     */
    public function ajaxMember() {
        $name = input("name/s");
        $mobile = input("mobile/s");
        $card_no = input("card_no/s");
        $card_type = input("card_type/d");
        $shop_id = input("shop_id/d");
        $api_version = input("api_version/s");
        $where = array("owner_type" => array("eq", 1)); //会员卡
        if (!empty($name)) {
            $where["i.name"] = array("like", "%{$name}%");
        }
        if (!empty($mobile)) {
            $where["i.mobile"] = array("like", "%{$mobile}%");
        }
        if (!empty($card_no)) {
            $where["i.card_no"] = array("like", "%{$card_no}%");
        }
        if (!empty($card_type)) {
            $where["i.card_type"] = array("eq", $card_type);
        }

        if (!empty($shop_id)) {
            $where["i.shop_id"] = array("eq", $shop_id);
        }
        if (!empty($api_version)) {
            $where["s.api_version"] = array("eq", $api_version);
        }
        $draw = input("draw",0) + 1;//请求时间

        $total = db("interface")->alias("i")
            ->join("m_shop s", "i.shop_id = s.id", "LEFT")
            ->where($where)
            ->count(1);
        $field = "i.id,i.card_no, i.in_varifycation, i.in_varifycation_type, i.mobile, i.name, i.card_type, s.shop_name,s.id as shop_id";

        $list = db("interface")->alias("i")
            ->field($field)
            ->join("m_shop s", "i.shop_id = s.id", "LEFT")
            ->where($where)
            ->order("i.id desc")
            ->limit($this->get_limit())
            ->select();
        $this->ajax(array('pageData' => $list, 'total' => $total, "draw" => $draw));
    }

    /**
     * 入场记录
     */
    public function memberEnter()
    {
        $auth_group = sp_get_auth_menu_3("20011001");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $api_versions = db("api_template")->field("id, name, api_version")->select();
        $this->assign("api_versions", $api_versions);
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        $this->assign("now",date("Y-m-d"));
        echo $this->fetch();
    }

    /**
     * 入场记录数据
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function ajaxEnter() {

        $this->ajax($this->getMemberEnterData());
    }

    /**
     * 导出excle
     * */
    public function exportExcel(){
        $pageSize = input("pageSize");
        if (empty($pageSize)) {
            exit();
        }

        $titles_des = array("卡号", "姓名", "进场时间", "出场时间", "错误代码", "归属店铺", "店铺id", "接口版本");
        $titles_field = array("card_no", "name", "in_time", "out_time", "error_msg", "shop_name", "shop_id", "api_version");
        $data = $this->getMemberEnterData();
        $list=$data['pageData'];
//        foreach ($list as $key => $value) {
//            if($value['owner_type']==1){
//                $list[$key]['owner_type']="会员卡";
//            }else if($value['owner_type']==2){
//                $list[$key]['owner_type']="员工卡";
//            }else if($value['owner_type']==3){
//                $list[$key]['owner_type']="临时卡";
//            }
//        }

        $title = "会员入场记录";
        var_dump(export_excel($titles_des, $titles_field, $list, $title));
        die;
    }

    /**
     * 获取会员入场记录数据
     */
    private function getMemberEnterData()
    {
        $behavior_status = input("behavior_status/d");
        $card_no = input("card_no/d");
        $start = input("start/s");
        $end = input("end/s");
        $shop_id = input("shop_id/d");
        $in_out_status = input("in_out_status/d");
        $api_version = input("api_version/s");
        $draw = input("draw",0) + 1;//请求时间

        $where["bl.owner_type"] = array("eq", 1); //会员卡
        if (empty($start)) {
            $start = date('Y-m-d') . " 00:00:00";
        } else {
            $start = $start . " 00:00:00";
        }
        if (empty($end)) {
            $end = date('Y-m-d') . " 23:59:59";
        }else{
            $end = $end . " 23:59:59";
        }
        if ($end < $start) {
            $this->error("起始时间不能大于结束时间");
        }
        $where["bl.create_time"] = array("between time", array($start, $end));

        if (!empty($card_no)) {
            $where["bl.card_no"] = array("like", "%{$card_no}%");
        }
        if ( $in_out_status == 1) {// 已经出场
            $where["bl.out_time"] = array('exp','is not null');
        } else if ($in_out_status == 2) {  // 未出场
            $where["bl.out_time"] = array(array("eq", ""), null, "or");
        }
        $crash_where = array(
            "bl.create_time" => array("between time", array($start, $end)),
            "bl.behavior_status" => array("eq", 1),
            "bl.behavior_type" => array("eq", 1),
            "bl.owner_type" => array("eq", 1)
        );
        if (!empty($shop_id)) {
            $where["bl.shop_id"] = array("eq", $shop_id);
            $crash_where["bl.shop_id"] = array("eq", $shop_id);
        }
        if (!empty($api_version)) {
            $where["s.api_version"] = array("eq", $api_version);
            $crash_where["s.api_version"] = array("eq", $api_version);
        }
        if ($behavior_status == 1) {
            $total = db("behavior_log")->alias('bl')->join("m_shop s", "s.id = bl.shop_id", "left")->where($where)->count(1);
            $list = db("behavior_log")->alias('bl')
                ->join("m_shop s", "s.id = bl.shop_id", "left")
                ->field("bl.*, s.shop_name, s.api_version")
                ->where($where)
                ->order($this->get_order())
                ->limit($this->get_limit())
                ->select();
        } else {
            $total = db("behavior_log_error")->alias('bl')->join("m_shop s", "s.id = bl.shop_id", "left")->where($where)->count(1);
            $list = db("behavior_log_error")->alias('bl')
                ->join("m_shop s", "s.id = bl.shop_id", "left")
                ->field("bl.*, s.shop_name, s.api_version")
                ->where($where)
                ->order($this->get_order())
                ->limit($this->get_limit())
                ->select();
        }
		$statistic_where="1=1 and owner_type=1 and create_time>'$start' and create_time <'$end' ";
		if(!empty($shop_id)){
			$statistic_where.=" and shop_id=$shop_id";
		}
		if(!empty($card_no)){
			$statistic_where.=" and card_no='$card_no'";
		}
        //统计刷卡次数
        $crash_1 = db("behavior_log")
            ->where($statistic_where)
            ->count(1);
        //统计刷卡人数
        $crash_2 = db("behavior_log")
            ->where($statistic_where)
            ->count("distinct(card_no)");

        //统计未出场人次
        $statistic_where.=" and out_time is null";
        $crash_3 = db("behavior_log")
            ->where($statistic_where)
			->fetchSql(FALSE)
            ->count(1);
        return array('pageData' => $list, 'total' => $total, "draw" => $draw, "crash_1" => $crash_1, "crash_2" => $crash_2, "crash_3" => $crash_3);
    }
}